package nc.ui.levmmeampoundbill.poundbill.view;

import java.awt.LayoutManager;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.yonyou.le.unattended_hardware.autoread.AutoReadPrintTimer;
import com.yonyou.le.unattended_hardware.autoread.AutoReadSokect;
import com.yonyou.le.unattended_hardware.autoread.AutoReadSokectMore;
import com.yonyou.le.unattended_hardware.autoread.LEDAndIOControlerUtils;
import com.yonyou.le.unattended_hardware.autoread.PurchaseAutoReadCard;
import com.yonyou.le.unattended_hardware.device.utils.DeviceUtils;

import nc.bs.logging.Logger;
import nc.bs.uif2.validation.IValidationService;
import nc.itf.web.service.QuerySeriveStatic;
import nc.ui.levmmeampoundbill.poundbill.editor.PoundTemplateContainer;
import nc.ui.levmmeampoundbill.poundbill.handler.pound.VehicleAfterEditEventHandler;
import nc.ui.levmmeampoundbill.poundbill.handler.setting.EWAfterEditEventHandler;
import nc.ui.levmmeampoundbill.poundbill.helper.MeamMaterialHelper;
import nc.ui.levmmeampoundbill.poundbill.helper.PoundNumHelper;
import nc.ui.levmmeampoundbill.poundbill.helper.ReadEWDataHelper;
import nc.ui.levmmeampoundbill.poundbill.model.PoundSettingBillManageModel;
import nc.ui.mmgp.uif2.view.MMGPShowUpableBillForm;
import nc.ui.pub.beans.UIRefPane;
import nc.ui.pub.bill.BillCardLayout;
import nc.ui.pub.bill.BillCardPanel;
import nc.ui.pub.bill.BillItem;
import nc.ui.pubapp.uif2app.model.BillListManageModel;
import nc.ui.uif2.ToftPanelAdaptor;
import nc.vo.levm.adaptor.entity.DeviceInfo;
import nc.vo.levmmeampoundbill.poundbill.AggPoundBillVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillBVO;
import nc.vo.levmmeampoundbill.poundbill.PoundBillVO;
import nc.vo.mmgp.util.MMArrayUtil;
import nc.vo.mmgp.util.MMNumUtil;
import nc.vo.mmgp.util.MMStringUtil;
import nc.vo.pub.bill.BillTempletVO;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class PoundBillForm extends MMGPShowUpableBillForm {

    private static final long serialVersionUID = -1852977433687495755L;

    public static final String DEFUALT_TMEPLATE = "DefaultTemplate";

    public static final String HEAD_FLAG = "_HEAD";

    public static final String BODY_FLAG = "_BODY";
    
    public Timer timer;
    
    public Timer printtimer;
  //addby_王格萍_2018年11月23日_begin
    private PoundSettingForm settingEditor;
	
	public PoundSettingForm getSettingEditor() {
		return settingEditor;
	}

	public void setSettingEditor(PoundSettingForm settingEditor) {
		this.settingEditor = settingEditor;
	}
    private IValidationService readvalidationService;
    
    public IValidationService getReadvalidationService() {
		return readvalidationService;
	}

	public void setReadvalidationService(IValidationService readvalidationService) {
		this.readvalidationService = readvalidationService;
	}
	private VehicleAfterEditEventHandler vehicleAfterEditEventHandler;
    
    public VehicleAfterEditEventHandler getVehicleAfterEditEventHandler() {
		return vehicleAfterEditEventHandler;
	}

	public void setVehicleAfterEditEventHandler(
			VehicleAfterEditEventHandler vehicleAfterEditEventHandler) {
		this.vehicleAfterEditEventHandler = vehicleAfterEditEventHandler;
	}
	private BillListManageModel unFinPoundAppModel;
    
    public BillListManageModel getUnFinPoundAppModel() {
		return unFinPoundAppModel;
	}

	public void setUnFinPoundAppModel(BillListManageModel unFinPoundAppModel) {
		this.unFinPoundAppModel = unFinPoundAppModel;
	}
	private MeamMaterialHelper materialHelper;
    
    public MeamMaterialHelper getMaterialHelper() {
		return materialHelper;
	}

	public void setMaterialHelper(MeamMaterialHelper materialHelper) {
		this.materialHelper = materialHelper;
	}
	private PoundNumHelper poundNumHelper;

	private IValidationService validationService;

	public PoundNumHelper getPoundNumHelper() {
		return poundNumHelper;
	}

	public void setPoundNumHelper(PoundNumHelper poundNumHelper) {
		this.poundNumHelper = poundNumHelper;
	}

	public IValidationService getValidationService() {
		return validationService;
	}

	public void setValidationService(IValidationService validationService) {
		this.validationService = validationService;
	}
    private ReadEWDataHelper readEWdataHelper;
    
    public ReadEWDataHelper getReadEWdataHelper() {
		return readEWdataHelper;
	}

	public void setReadEWdataHelper(ReadEWDataHelper readEWdataHelper) {
		this.readEWdataHelper = readEWdataHelper;
	}

	private EWAfterEditEventHandler ewAfterEditEventHandler;
    
    public EWAfterEditEventHandler getEwAfterEditEventHandler() {
		return ewAfterEditEventHandler;
	}

	public void setEwAfterEditEventHandler(
			EWAfterEditEventHandler ewAfterEditEventHandler) {
		this.ewAfterEditEventHandler = ewAfterEditEventHandler;
	}

	private SerialDataPanel measPanel;
    
    public SerialDataPanel getMeasPanel() {
		return measPanel;
	}

	public void setMeasPanel(SerialDataPanel measPanel) {
		this.measPanel = measPanel;
	}

	private PoundSettingBillManageModel settingModel;
    
    public PoundSettingBillManageModel getSettingModel() {
		return settingModel;
	}

	public void setSettingModel(PoundSettingBillManageModel settingModel) {
		this.settingModel = settingModel;
	}

	private PoundBillForm editor;
    
    public PoundBillForm getEditor() {
		return editor;
	}

	public void setEditor(PoundBillForm editor) {
		this.editor = editor;
	}
	//addby_王格萍_2018年11月23日_end
    /**
     * 缓存BillCardPanel<key=交易类型编码，billCardPanel>
     */
    private Map<String, BillCardPanel> billCardPanelCache = new HashMap<String, BillCardPanel>();

    /**
     * 缓存模板初始状态可编辑Item<key=交易类型编码+head/body,Set<String>>
     */
    private Map<String, Set<String>> editableItemCache = new HashMap<String, Set<String>>();

    private String tranTypeCode;

    public PoundBillForm() {
        super();
    }

    
    public Timer getTimer() {
		return timer;
	}
    
    public Timer getPrintTimer() {
		return printtimer;
	}
    
    private Map<String, DeviceInfo> dMap;

    private DeviceInfo threadD;
    private DeviceInfo threadD2;
    
    
	@Override
    public void initUI() {

        if (MMStringUtil.isEmpty(tranTypeCode)) {
            setTranTypeCode(getNodeDefaultTranTypeCode());
        }
        if (MMStringUtil.isEmpty(getTranTypeCode())) {
            return;
        }
        super.initUI();
        LayoutManager layout = getBillCardPanel().getLayout();
        if (layout instanceof BillCardLayout) {
            BillCardLayout billCardLayout = (BillCardLayout) layout;
            billCardLayout.setHeadScale(60);
            getBillCardPanel().setLayout(billCardLayout);
            billCardLayout.layoutContainer(getBillCardPanel());
        }
        // UIPanel headPanel = getBillCardPanel().getHeaderPanel();
        // int count = headPanel.getComponentCount();
        // for(int i=0;i<count;i++){
        // Component component = headPanel.getComponent(i);
        // if(component instanceof BillAreaPanel){
        // ((BillAreaPanel) component).setLayout(mgr)
        // }
        // }
        if("4E10032004".equals(getModel().getContext().getNodeCode())){
            //打开计量单节点初始化计量单自动读卡线程
//            initDeveice();
//            //调用打印
//            AggDeliveryBillVO aggvo = new AggDeliveryBillVO();
//            DeliveryBillHeadVO hvo = new DeliveryBillHeadVO();
//            hvo.setVdef1("190610886");
//            aggvo.setParent(hvo);
//            new BaseMetaDataBasedPrintAction()
//            	.doPrint(new AggDeliveryBillVO[]{aggvo}, "0001A11000000000040C", "4E10028008", "0001A110000000001M6R", "card");
        }
    }

    private void initDeveice() {
    	// add by王格萍_采购ic卡和rfid卡实现自动读卡，需注入参数_2019年6月6日_begin
    	DeviceUtils.getInstance().setModel(this.getModel());
    	DeviceUtils.getInstance().setEditor(this.getEditor());
    	DeviceUtils.getInstance().setPanel(this.billCardPanel);
    	DeviceUtils.getInstance().setSettingModel(this.getSettingModel());
    	DeviceUtils.getInstance().setMeasPanel(this.getMeasPanel());
    	DeviceUtils.getInstance().setEwAfterEditEventHandler(this.getEwAfterEditEventHandler());
    	DeviceUtils.getInstance().setReadEWdataHelper(this.getReadEWdataHelper());
    	DeviceUtils.getInstance().setPoundNumHelper(this.getPoundNumHelper());
    	DeviceUtils.getInstance().setValidationService(this.getValidationService());
    	DeviceUtils.getInstance().setMaterialHelper(this.getMaterialHelper());
    	DeviceUtils.getInstance().setUnFinPoundAppModel(this.getUnFinPoundAppModel());
    	DeviceUtils.getInstance().setVehicleAfterEditEventHandler(this
    			.getVehicleAfterEditEventHandler());
    	DeviceUtils.getInstance().setReadvalidationService(this.getReadvalidationService());
    	DeviceUtils.getInstance().setSettingEditor(this.getSettingEditor());
    	//打开节点初始化红绿灯及led
    	boolean light1=LEDAndIOControlerUtils.getInstanse().light1Red();
    	if(light1==Boolean.FALSE){
    		Logger.error("控制红绿灯失败");
    	}
    	boolean light2=LEDAndIOControlerUtils.getInstanse().light2Green();
    	if(light2==Boolean.FALSE){
    		Logger.error("控制红绿灯失败");
    	}
		LEDAndIOControlerUtils.getInstanse().sendToLED("    景兴纸业欢迎你！");
    	// add by王格萍_采购ic卡和rfid卡实现自动读卡，需注入参数_2019年6月6日_end
//    	if(timer == null){
//    		timer = new Timer();
//    		timer.schedule(new AutoReadTimer(), 5000, 3000);//启动自动读卡线程，1.5秒轮询一次
//    	}
		List<DeviceInfo> devInfos = new PurchaseAutoReadCard().getReadUtils().getDeviceInfo(QuerySeriveStatic.CARDREADLIBCODE);
		if(devInfos!=null &&devInfos.size()>0) {
			for (DeviceInfo devInfo : devInfos) {
				String def2 = devInfo.getDef2();
				if(def2!=null){
					Socket socket=null;
					try {
						socket = new Socket("localhost",Integer.parseInt(devInfo.getDeviceCode())+42000);
					} catch (Exception e) {
						// TODO: handle exception
					}
					if(socket!=null&&!socket.isClosed()){
						try {
						socket.close();
						} catch (IOException e) {
							ExceptionUtils.wrappBusinessException("initDeveice1:"+e.getMessage());
						}
				}
			}
			}
		}

		if(devInfos!=null &&devInfos.size()>1){
			threadD = devInfos.get(0);
			threadD2 = devInfos.get(1);
				getThreadPool().execute(new Runnable() {
					@Override
					public void run() {
						try {
							AutoReadSokectMore autoReadSokectMore = new AutoReadSokectMore(threadD); 
							autoReadSokectMore.AutoRead(threadD);
						}catch (Exception e) {
							ExceptionUtils.wrappBusinessException("initDeveice2:"+e.getMessage());
						}
					}
				});
				getThreadPool().execute(new Runnable() {
					@Override
					public void run() {
						try {
							AutoReadSokectMore autoReadSokectMore = new AutoReadSokectMore(threadD2); 
							autoReadSokectMore.AutoRead(threadD2);
						}catch (Exception e) {
							ExceptionUtils.wrappBusinessException("initDeveice3:"+e.getMessage());
						}
					}
				});
		}else{
			getThreadPool().execute(new Runnable() {
				@Override
				public void run() {
					try {
						AutoReadSokect autoReadSokect = new AutoReadSokect(); 
						autoReadSokect.AutoRead();
					}catch (Exception e) {
						ExceptionUtils.wrappBusinessException("initDeveice4:"+e.getMessage());
					}
				}
			});
		}
    	if(printtimer == null){
			printtimer = new Timer();
			printtimer.schedule(new AutoReadPrintTimer(), 5000, 3000);//启动自动读卡线程，1.5秒轮询一次
		}
		
	}
    
    // 线程池
 	private static ExecutorService threadPool;
    /**
	 * 单例线程池
	 * 
	 * @return
	 */
	private ExecutorService getThreadPool() {
		if (threadPool == null)
			threadPool = Executors.newCachedThreadPool();
		return threadPool;
	}
    
	/**
     * 很据交易类型编码，重置卡片界面
     * 
     * @param nodeCode
     */
    public void resetBillCardPanel(String tranTypeCode) {

        this.removeAll();
        this.tranTypeCode = tranTypeCode;
        if (billCardPanelCache.get(tranTypeCode) != null) {
            this.billCardPanel = billCardPanelCache.get(tranTypeCode);
            if (componentValueManager != null) {
                componentValueManager.setComponent(billCardPanel);
            }
            add(billCardPanel);
        } else {
            this.initUI();
            billCardPanelCache.put(tranTypeCode, billCardPanel);
            initEditableItemCache(tranTypeCode, billCardPanel);
            
        }

    }

    protected void initEditableItemCache(String tranTypeCode,
                                         BillCardPanel billCardPanel) {
        BillItem[] headItems = billCardPanel.getHeadItems();
        editableItemCache.put(tranTypeCode + HEAD_FLAG, getEditItemKeys(tranTypeCode, headItems));

        BillItem[] bodyItems = billCardPanel.getBodyItems();
        editableItemCache.put(tranTypeCode + BODY_FLAG, getEditItemKeys(tranTypeCode, bodyItems));

    }

    /**
     * @param tranTypeCode
     * @param headItems
     */
    private Set<String> getEditItemKeys(String tranTypeCode,
                                        BillItem[] headItems) {
        Set<String> editableItems = new HashSet<String>();
        for (BillItem billItem : headItems) {
            if (billItem.isEdit()) {
                editableItems.add(billItem.getKey());
            }
        }
        return editableItems;
    }

    /**
     * 创建模板VO
     * 
     * @see nc.ui.uif2.editor.BillForm#createBillTempletVO()
     */
    @Override
    protected BillTempletVO createBillTempletVO() {

        BillTempletVO template = null;

        if (getTranTypeCode() != null && getTranTypeCode() != DEFUALT_TMEPLATE) {
            template = getPoundTemplateContainer().getPoundTemplate(getTranTypeCode(), getPos(), getTabCode());

            getPoundTemplateContainer().setDefaultTemplet(getTranTypeCode());
        }

        // 如果通过交易类型无法查找到对应模版，应加载默认模版
        if (template == null) {
            template = getDefaultBillTempletVO();

            getPoundTemplateContainer().setDefaultTemplet(getNodekey());
        }

        return template;
    }

    public BillTempletVO getDefaultBillTempletVO() {
        return getPoundTemplateContainer().getTemplate(getNodekey());
    }

    /**
     * 获取当前节点的交易类型
     * 
     * @return
     */
    public String getNodeDefaultTranTypeCode() {
        return ((ToftPanelAdaptor) getModel().getContext().getEntranceUI()).getParameter("transtype");
    }

    public PoundTemplateContainer getPoundTemplateContainer() {
        return (PoundTemplateContainer) getTemplateContainer();
    }

    public String getTranTypeCode() {
        return tranTypeCode;
    }

    public void setTranTypeCode(String tranTypeCode) {
        this.tranTypeCode = tranTypeCode;
    }

    /**
     * 为磅单界面字段赋值
     * 
     * @param itemKeyo
     * @param value
     */
    public void setPoundHeadItemValue(String itemKey,
                                      Object value) {
        if (getPoundHeadItem(itemKey) != null) {
            getBillCardPanel().setHeadItem(itemKey, value);
        }
    }

    /**
     * 为磅单界面表体赋值
     * 
     * @param rowIndex
     * @param strKey
     * @return
     */
    public Object getPoundBodyItemValue(int rowIndex,
                                        String strKey) {
        PoundBillBVO poundBodyVO =
                (PoundBillBVO) this
                    .getBillCardPanel()
                    .getBillModel()
                    .getBodyValueRowVO(rowIndex, PoundBillBVO.class.getName());
        return poundBodyVO != null ? poundBodyVO.getAttributeValue(strKey) : null;
    }

    /**
     * 获得磅单界面BillItem
     * 
     * @param itemKey
     * @return
     */
    public BillItem getPoundHeadItem(String itemKey) {
        BillItem billItem = getBillCardPanel().getHeadItem(itemKey);
        if (billItem == null) {
            billItem = getBillCardPanel().getTailItem(itemKey);
        }
        return billItem;

    }

    /**
     * 获得磅单界面item值
     * 
     * @param itemKey
     * @return
     */
    public Object getPoundHeadItemValue(String itemKey) {
        return getPoundHeadItem(itemKey) == null ? null : getPoundHeadItem(itemKey).getValueObject();
    }

    /**
     * 设置表体Item可编辑状态
     * 
     * @param itemName
     * @param enable
     */
    public void setPoundBodyItemEditable(String itemName,
                                         boolean enable) {
        if (MMStringUtil.isEmpty(itemName)) {
            return;
        }
        setBodyItemEditEnable(this.getBillCardPanel(), new String[]{itemName }, enable);

    }

    /**
     * 设置表体可编辑状态
     * 
     * @param itemNames
     * @param enable
     */
    public void setPoundBodyItemsEditable(String[] itemNames,
                                          boolean enable) {
        setBodyItemEditEnable(this.getBillCardPanel(), itemNames, enable);

    }

    /**
     * 设置item可编辑属性
     * 
     * @param itemNames
     * @param enable
     *        :可编辑属性
     */
    protected void setBodyItemEditEnable(BillCardPanel cardPanel,
                                         String[] itemNames,
                                         boolean enable) {
        if (itemNames == null || itemNames.length == 0) {
            return;
        }
        for (String itemName : itemNames) {
            if (cardPanel.getBodyItem(itemName) != null) {
                cardPanel.getBodyItem(itemName).setEdit(enable);
            }
        }
    }

    /**
     * 设置磅单item可编辑属性
     * 
     * @param itemNames
     * @param enable
     */
    public void setPoundHeadItemEditable(String[] itemNames,
                                         boolean enable) {
        setHeadItemEditEnable(getBillCardPanel(), itemNames, enable);
    }

    /**
     * 设置磅单item可编辑属性
     * 
     * @param itemName
     * @param enable
     */
    public void setPoundHeadItemEditable(String itemName,
                                         boolean enable) {
        if (MMStringUtil.isEmpty(itemName)) {
            return;
        }
        setPoundHeadItemEditable(new String[]{itemName }, enable);
    }

    /**
     * 设置item可编辑属性
     * 
     * @param itemNames
     * @param enable
     *        :可编辑属性
     */
    protected void setHeadItemEditEnable(BillCardPanel cardPanel,
                                         String[] itemNames,
                                         boolean enable) {
        if (itemNames == null || itemNames.length == 0) {
            return;
        }
        for (String itemName : itemNames) {
            if (cardPanel.getHeadItem(itemName) != null) {
                cardPanel.getHeadItem(itemName).setEdit(enable);
            } else if (cardPanel.getTailItem(itemName) != null) {
                cardPanel.getTailItem(itemName).setEdit(enable);
            }
        }
    }

    /**
     * 重新设置PoundCardPanel
     * 
     * @see nc.ui.pubapp.uif2app.view.BillForm#createBillCardPanel()
     */
    @SuppressWarnings("restriction")
    @Override
    protected void createBillCardPanel() {
        this.billCardPanel = new PoundCardPanel();
        if (this.billCardPanel instanceof nc.ui.pubapp.bill.BillCardPanel) {
            ((nc.ui.pubapp.bill.BillCardPanel) this.billCardPanel).setBillForm(this);
        }
    }

    /**
     * 拦截车牌号参照
     * 
     * @see nc.ui.pubapp.uif2app.view.ShowUpableBillForm#setValue(java.lang.Object)
     */
    @SuppressWarnings("restriction")
    @Override
    public void setValue(Object object) {

        super.setValue(object);
        if (object instanceof AggPoundBillVO) {
            AggPoundBillVO aggPoundBillVO = (AggPoundBillVO) object;
            if (aggPoundBillVO != null && aggPoundBillVO.getParent() != null) {

                getBillCardPanel().setHeadItem(PoundBillVO.VVEHICLEREF, aggPoundBillVO.getParentVO().getVvehicle());

            }
        }

    }

    /**
     * 设置聚合VO到磅单表体
     * 
     * @param aggPoundBill
     * @return
     */
    public AggPoundBillVO setPoundBillVOToView(AggPoundBillVO aggPoundBill) {
        complementAggPoundVO(aggPoundBill);

        getBillCardPanel().setBillValueVO(aggPoundBill);

        // 单独设置界面数据
        if (aggPoundBill.getParentVO() != null) {
            ((UIRefPane) getPoundHeadItem(PoundBillVO.VVEHICLEREF).getComponent()).setValue(aggPoundBill
                .getParentVO()
                .getVvehicle());

        }
        return aggPoundBill;
    }

    /**
     * 填充磅单VO表头和表体字段
     * 
     * @param aggPoundBill
     */
    public AggPoundBillVO complementAggPoundVO(AggPoundBillVO aggPoundBill) {
        if (aggPoundBill != null && aggPoundBill.getParentVO() != null) {
            // 将表体中字段设置到表头
            PoundBillVO hvo = aggPoundBill.getParentVO();
            hvo.setVvehicleref(hvo.getVvehicle());

            PoundBillBVO[] bvos = aggPoundBill.getChildrenVO();
            if (!MMArrayUtil.isEmpty(bvos)) {
                PoundBillBVO bvo = bvos[0];
                // 计量单位
                hvo.setCmainunitid(bvo.getCmainunitid());
                // 物料
                hvo.setCmaterialoid(bvo.getCmaterialoid());
                hvo.setCmaterialvid(bvo.getCmaterialvid());
                // 客商和客户
                hvo.setPk_customer(bvo.getPk_customer());
                hvo.setPk_supplier(bvo.getPk_supplier());
                // 收发货库存组织和仓库
                hvo.setPk_sendorg(bvo.getPk_sendorg());
                hvo.setPk_sendorg_v(bvo.getPk_sendorg_v());
                hvo.setPk_sendstore(bvo.getPk_sendstore());
                hvo.setPk_receiveorg(bvo.getPk_receiveorg());
                hvo.setPk_receiveorg_v(bvo.getPk_receiveorg_v());
                hvo.setPk_receivestore(bvo.getPk_receivestore());
            }
        }
        return aggPoundBill;
    }

    /**
     * <b>从磅单界面获得磅单VO</b>
     * <p>
     * (不包含过磅设置界面数据信息)
     * </p>
     * 
     * @return
     */
    public AggPoundBillVO getPoundBillVOFromView() {

        AggPoundBillVO aggVO = (AggPoundBillVO) getBillCardPanel().getBillData().getBillObjectByMetaData();
        return aggVO;
    }

    /**
     * 重置当前榜单面板的可编辑状态
     */
    public void resetCardPanelToInitEditState() {

        // 获得表头可编辑项
        Set<String> headItemEditKeys = editableItemCache.get(getTranTypeCode() + HEAD_FLAG);
        // 获得表头不可编辑项
        Set<String> headUneditableItemKeys = getUnEditableItemKeys(headItemEditKeys, billCardPanel.getHeadItems());
        // 分别重置可编辑项和不可编辑项
        setPoundHeadItemEditable(headItemEditKeys.toArray(new String[0]), true);
        setPoundHeadItemEditable(headUneditableItemKeys.toArray(new String[0]), false);

        // 表体同上
        Set<String> bodyItemEditKeys = editableItemCache.get(getTranTypeCode() + BODY_FLAG);
        Set<String> bodyUneditableItemKeys = getUnEditableItemKeys(bodyItemEditKeys, billCardPanel.getBodyItems());
        setPoundBodyItemsEditable(bodyItemEditKeys.toArray(new String[0]), true);
        setPoundBodyItemsEditable(bodyUneditableItemKeys.toArray(new String[0]), false);

    }

    protected Set<String> getUnEditableItemKeys(Set<String> enableItemKeys,
                                                BillItem[] billItems) {

        Set<String> unEitableItems = new HashSet<String>();
        for (BillItem item : billItems) {

            if (!enableItemKeys.contains(item.getKey())) unEitableItems.add(item.getKey());
        }
        return unEitableItems;
    }

    /**
     * 判断表体某字段是否有值
     * 
     * @param poundBillVO
     * @param itemKey
     * @return
     */
    public boolean checkBodyItmeIsExistValues(AggPoundBillVO poundBillVO,
                                              String itemKey) {
        if (MMArrayUtil.isEmpty(poundBillVO.getChildrenVO())) {
            return false;
        }
        for (PoundBillBVO bvo : poundBillVO.getChildrenVO()) {
            boolean isExistValue = checkBodyItemIsExistValue(bvo, itemKey);
            if (isExistValue) {
                return true;
            }
        }
        return false;

    }

    /**
     * @param itemKey
     * @param bvo
     */
    private boolean checkBodyItemIsExistValue(PoundBillBVO bvo,
                                              String itemKey) {
        boolean isNull = true;
        Object obj = bvo.getAttributeValue(itemKey);
        if (obj instanceof String) {
            isNull = MMStringUtil.isEmpty((String) obj);

        } else if (obj instanceof UFDouble) {

            isNull = MMNumUtil.objToUFDouble(obj, UFDouble.ZERO_DBL).equals(UFDouble.ZERO_DBL);
        } else {
            isNull = obj == null;
        }
        return !isNull;
    }

    @Override
    public void showMeUp() {
        // 卡片界面显示时，补充表头计算属性字段信息。如：表头的计量单位，控制表头字段的精度
        AggPoundBillVO aggVO = (AggPoundBillVO) getModel().getSelectedData();

        complementAggPoundVO(aggVO);

        super.showMeUp();

    }
}
